﻿using MediatR;
using ProductionLine.Domain.Product;
using Sale.Api.Application.Command.Discount;
using Sale.Domain;
using Sale.Dtos;
using Sale.ErrorCode;
using Sale.Infrastructure.Interface;

namespace Sale.Api.Application.Handler.Discount
{
    public class GetDiscountHandler : IRequestHandler<GetDiscountCommand, ApiResult<List<DiscountDTO>>>
    {
        private readonly IBaseRepository<ClientPriceModel> clientPriceRepo;
        private readonly IBaseRepository<ProductModel> productRepo;
        private readonly ILogger<GetDiscountHandler> logger;

        public GetDiscountHandler(IBaseRepository<ClientPriceModel> clientPriceRepo,IBaseRepository<ProductModel> productRepo,ILogger<GetDiscountHandler> logger)
        {
            this.clientPriceRepo = clientPriceRepo;
            this.productRepo = productRepo;
            this.logger = logger;
        }
        public Task<ApiResult<List<DiscountDTO>>> Handle(GetDiscountCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<DiscountDTO>> result = new ApiResult<List<DiscountDTO>>();
            var clientPrice = clientPriceRepo.GetAll().Where(x => x.IsDel == false);
            var product = productRepo.GetAll();
            var linq = from a in clientPrice
                       join b in product
                       on a.ProductCode equals b.ProductCode
                       select new DiscountDTO
                       {
                           DiscountAmount = a.DiscountAmount,
                           ClientPriceId = a.ClientPriceId,
                           DiscountName = a.DiscountName,
                           ProductCode = b.ProductCode,
                           ProductName=b.ProductName,
                           Specification=b.Specification,
                       };
            result.Data = linq.ToList();
            result.Code= ApiEnum.OK;
            result.Msg = "折扣查询成功";
            logger.LogInformation(result.Msg);
            return Task.FromResult(result);
        }
    }
}
